﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MiniScrum.Models
{
    public class ScrumSummary
    {
        public Dictionary<int, string> CompletedTasks { get; set; }
        public Dictionary<int, string> PendingTasks { get; set; }
        public Dictionary<int, string> Blockers { get; set; }
        public KeyValuePair<int, string> UserInformation { get; set; }


        public static ScrumSummary GetTodayDetails(int userId)
        {
            using (MiniScrumDB db = new MiniScrumDB())
            {
                ScrumSummary result = new ScrumSummary();
                DateTime currendDate = DateTime.UtcNow.Date;
                User user = db.Users.Find(userId);
                List<Task> completedTasks = user.Tasks
                    .Where(t => t.CompletedDate != null && t.CompletedDate > currendDate.AddDays(-2)).ToList();

                List<Task> pendingTasks = user.Tasks
                    .Where(t => t.CompletedDate == null).ToList();
                List<Blocker> blockers = user.Blockers
                    .Where(b => b.CreatedDate == currendDate).ToList();

                result.UserInformation = new KeyValuePair<int, string>(userId, user.FullName);
                result.CompletedTasks = new Dictionary<int, string>();
                completedTasks.ForEach(ct =>
                {
                    result.CompletedTasks.Add(ct.Id, ct.Name);
                });

                result.PendingTasks = new Dictionary<int, string>();
                pendingTasks.ForEach(ct =>
                {
                    result.PendingTasks.Add(ct.Id, ct.Name);
                });

                result.Blockers = new Dictionary<int, string>();
                blockers.ForEach(ct =>
                {
                    result.Blockers.Add(ct.Id, ct.Name);
                });

                return result;
            }
        }

        public static List<ScrumSummary> GetAllDetails()
        {
            using (MiniScrumDB db = new MiniScrumDB())
            {
                List<ScrumSummary> result = new List<ScrumSummary>();
                DateTime currendDate = DateTime.UtcNow.Date;

                foreach (User user in db.Users)
                {
                    ScrumSummary summary = new ScrumSummary();
                    List<Task> completedTasks = user.Tasks
                               .Where(t => t.CompletedDate != null && t.CompletedDate > currendDate.AddDays(-2)).ToList();

                    List<Task> pendingTasks = user.Tasks
                        .Where(t => t.CompletedDate == null).ToList();
                    List<Blocker> blockers = user.Blockers
                        .Where(b => b.CreatedDate == currendDate).ToList();

                    summary.UserInformation = new KeyValuePair<int, string>(user.Id, user.FullName);
                    summary.CompletedTasks = new Dictionary<int, string>();
                    completedTasks.ForEach(ct =>
                    {
                        summary.CompletedTasks.Add(ct.Id, ct.Name);
                    });

                    summary.PendingTasks = new Dictionary<int, string>();
                    pendingTasks.ForEach(ct =>
                    {
                        summary.PendingTasks.Add(ct.Id, ct.Name);
                    });

                    summary.Blockers = new Dictionary<int, string>();
                    blockers.ForEach(ct =>
                    {
                        summary.Blockers.Add(ct.Id, ct.Name);
                    });

                    result.Add(summary);
                }

                return result;
            }
        }


    }
}
